<?php
class UserModel extends Zend_Db_Table_Abstract {
	
	protected $_name = 'user';
	protected $_id = 'id';
	
	public static function register($username, $password, $fullname, $email, $avatar, $music, $sport, $addressId, $sex, $birthday){
		$db = Zend_Registry::get('connectDB');
		$data = array('username'=> $username,'password'=> $password, 'fullname'=>$fullname, 'email'=>$email, 'avatar'=>$avatar, 'music'=>$music, 'sport'=>$sport, 'address'=> $addressId,'sex'=> $sex,'birthday'=> $birthday);
		$db->insert('user', $data);
	}
	public static function check($username ){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db->select()->from('user')->where("username='".$username."'");
		return $db->fetchAll($mysql);
	}
	
	public function getuserinfoID($id){
		$model = new UserModel();
		$info = $model->fetchRow($model->select()->where('id = ?',$id));
		return $info;
	}
	public function getuserinfo($username){
		$model = new UserModel();
		$info = $model->fetchRow($model->select()->where('username = ?',$username));
		return $info;
	}
	
	public function editdescription($id,$description){
		$model = new UserModel();
		$data = array('description'=>$description);
		$model->update($data,"id=".$id);
	}
	
	public function listall(){
		$model = new UserModel();
		$list = $model->fetchAll();
		return $list;
	}
	
	public function changeProfile( $id, $fullname, $email, $addressId, $sex, $dob, $music, $sport ){
		$db = Zend_Registry::get('connectDB');
		$data = array(  'fullname' => $fullname, 'email' => $email, 'address'=>$addressId,  'sex' => $sex, 'birthday' => $dob, 'music' => $music, 'sport'  => $sport);
		$db->update( 'user',$data, "id=".$id );
	}

	
	public function search($key){
		$model = new UserModel();
		$list = $model->fetchAll($model->select()->where("username LIKE '%$key%' OR fullname LIKE '%$key%'"));
		return $list;		
	}
	
	public function countsamehobby($user_id,$friend_id){
		$model = new UserModel();
		$info_user = $model->fetchRow($model->select()->where('id = '.$user_id));
		$music_user = explode('-',$info_user['music']);
		$sport_user = explode('-',$info_user['sport']);
		$info_friend = $model->fetchRow($model->select()->where('id = '.$friend_id));
		$music_friend = explode('-',$info_friend['music']);
		$sport_friend = explode('-',$info_friend['sport']);
		$m = array_intersect($music_user,$music_friend);
		$s = array_intersect($sport_user,$sport_friend);
		$count['music'] = implode('-',$m);
		$count['sport'] = implode('-',$s);
		$count['count'] = count($m)+count($s);
		return $count;
	}
	public function changeAvatar( $id, $avatar ){
		$db = Zend_Registry::get('connectDB');
		$data = array(  'avatar'  => $avatar);
		$db->update( 'user',$data, "id=".$id );
		
	}
	
}